package top.zuli.package01.class14;

public class DeadLock {
    public static void main(String[] args) {
        //1.定义两个锁
        Object obj1 = new Object();
        Object obj2 = new Object();
        //2.定义两个线程
        new Thread(){
            @Override
            public void run() {
                synchronized (obj1){
                    System.out.println(getName()+"使用obj1这个锁,即将调用obj2锁，等待释放");
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (obj2){
                        System.out.println(getName()+"使用obj2锁中...");
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                synchronized (obj2){
                    System.out.println(getName()+"使用obj2这个锁,即将调用obj1锁，等待释放");
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (obj1){
                        System.out.println(getName()+"使用obj1锁中...");
                    }
                }
            }
        }.start();
    }
}